package com.hj.chartdemo.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.data.Entry
import com.hj.chartdemo.R
import com.hj.chartdemo.bean.chart.ChartData
import com.hj.chartdemo.chart.BarChartManagger
import com.hj.chartdemo.chart.LineChartManagger
import com.hj.chartdemo.databinding.ChatLineBarViewBinding
import java.util.ArrayList

/**
 * 拆线图、柱状图切换
 */
class ChatLineBarView : LinearLayout {
    var activity: AppCompatActivity? = null

    var binding: ChatLineBarViewBinding? = null

    var mContext: Context? = null

    var showLine = true
    var onlyLine = false

    init {
        activity = context as AppCompatActivity
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context
        binding = ChatLineBarViewBinding.inflate(LayoutInflater.from(getContext()), this, true)
        val a = context.obtainStyledAttributes(attrs, R.styleable.ChatLineBarView)
        var s = a.getInt(R.styleable.ChatLineBarView_showLine, 1)
        onlyLine = a.getBoolean(R.styleable.ChatLineBarView_onlyLine, false)
        showLine = s == 1
        initView()
    }

    private fun initView() {
        LineChartManagger.getInstance().init(this.context,binding?.lineChart)
        BarChartManagger.getInstance().init(mContext, binding?.barChart)
        if(!showLine){
            binding?.ivBar?.setImageResource(R.mipmap.zhuzhuangtu_xuanzhong)
            binding?.ivLine?.setImageResource(R.mipmap.quxiantu_weixuan)
            binding?.lineChart?.visibility = View.GONE
            binding?.barChart?.visibility = View.VISIBLE
        }
        binding?.ivBar?.setOnClickListener {
            if(showLine){
                binding?.ivBar?.setImageResource(R.mipmap.zhuzhuangtu_xuanzhong)
                binding?.ivLine?.setImageResource(R.mipmap.quxiantu_weixuan)

                showLine = false
                binding?.lineChart?.visibility = View.GONE
                binding?.barChart?.visibility = View.VISIBLE
            }
        }
        binding?.ivLine?.setOnClickListener {
            if(!showLine){
                binding?.ivBar?.setImageResource(R.mipmap.zhuzhuangtu_weixuan)
                binding?.ivLine?.setImageResource(R.mipmap.quxiantu_xuanzhong)

                showLine = true
                binding?.lineChart?.visibility = View.VISIBLE
                binding?.barChart?.visibility = View.GONE
            }
        }
        if(onlyLine){
            binding?.ivBar?.visibility = View.GONE
            binding?.ivLine?.visibility = View.GONE
        }
    }

    fun initShowLine(sl:Boolean){
        this.showLine = sl
    }

    fun setData(data: ChartData, ext:String) {
        var ztDatas = ArrayList<ArrayList<Entry>>()
        val values = ArrayList<Entry>()
        var xlab = ArrayList<String>()
        var index = 1

        data.datas.forEach {
            values.add(Entry(index.toFloat(), it.value.toFloat(),it.label))
            xlab.add(it.label)
            index ++
        }
        ztDatas.add(values)
        LineChartManagger.getInstance().loadData(mContext, binding?.lineChart,ztDatas,ArrayList<String>(),data.colors,xlab,data.showLegend,ext)

        data.colors[0] = Color.parseColor("#AEC7F0")
        BarChartManagger.getInstance().loadData(mContext, binding?.barChart,ztDatas,ArrayList<String>(),data.colors,xlab,data.showLegend)
    }

    fun setData(data: ChartData) {
        setData(data,"")
    }

}
